package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;


    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        // 存放begin到end范围内每天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            // 计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        // 存放每天的营业额
        List<Double> turnOverList=new ArrayList<>();
        for(LocalDate date:dateList){
            // 查询date日期对应的营业额数据,且状态为已完成的订单
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);  // 获取这一天的开始时刻(00:00:00)
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX); // 获取这一天的结束时刻(23:59:59)

            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);
            map.put("status", Orders.COMPLETED);
            Double turnOver=orderMapper.sumByMap(map);
            turnOver = turnOver == null ? 0 : turnOver;
            turnOverList.add(turnOver);
        }

        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnOverList,","))
                .build();
    }

    /**
     * 用户数据统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        // 存放begin到end范围内每天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            // 计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        // 存放用户数量
        List<Integer> totalUserNum=new ArrayList<>();
        // 存放新增用户数量
        List<Integer> newUserNum=new ArrayList<>();

        for(LocalDate date:dateList){
            // 每天开始时刻和结束时刻
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();

            // 总用户数量
            map.put("endTime",endTime);
            Integer totalUser=userMapper.countByMap(map);
            totalUser = totalUser == null ? 0 : totalUser;
            totalUserNum.add(totalUser);

            // 新增用户数量
            map.put("beginTime",beginTime);
            Integer newUser=userMapper.countByMap(map);
            newUser = newUser == null ? 0 : newUser;
            newUserNum.add(newUser);
        }
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserNum,","))
                .totalUserList(StringUtils.join(totalUserNum,","))
                .build();
    }

    /**
     * 订单数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        // 存放begin到end范围内每天的日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            // 计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        // 每日订单总数
        List<Integer> orderCountList=new ArrayList<>();
        // 每日有效订单数
        List<Integer> validOrderCountList=new ArrayList<>();
        // 时间区间内订单总数
        // Integer orderCounts=0;
        // 时间区间内有效订单数


        for (LocalDate date : dateList) {
            // 每天开始时刻和结束时刻
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();
            map.put("beginTime",beginTime);
            map.put("endTime",endTime);

            // 查询每天的订单总数
            Integer orderCount=orderMapper.countByMap(map);
            orderCount=orderCount == null ? 0 : orderCount;
            orderCountList.add(orderCount);

            // 查询每天的有效订单数
            map.put("status", Orders.COMPLETED);
            Integer vaildCount=orderMapper.countByMap(map);
            vaildCount=vaildCount == null ? 0 : vaildCount;
            validOrderCountList.add(vaildCount);
        }
        // 时间区间内的订单总数
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
        // 时间区间内的有效订单数量
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        // 订单完成率
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate=validOrderCount.doubleValue() / totalOrderCount;
        }
        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 销量排名前10
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        // 每天开始时刻和结束时刻
        LocalDateTime beginTime=LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime=LocalDateTime.of(end, LocalTime.MAX);

        // 存放销量排名前10
        List<GoodsSalesDTO> salesTop10=orderMapper.getSalesTop10(beginTime,endTime);

        List<String> names=salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numbers=salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        return SalesTop10ReportVO
                .builder()
                .nameList(StringUtils.join(names,","))
                .numberList(StringUtils.join(numbers,","))
                .build();
    }
}
